package com.novellotus.iov.carvice.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.novellotus.httploadingview.LoadingView;
import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.adapter.CarListAdapter;
import com.novellotus.iov.carvice.model.SearchCarModel;
import com.novellotus.iov.carvice.utils.MyViewUtils;
import com.novellotus.iov.carvice.view.PageView;
import com.orangegangsters.github.swipyrefreshlayout.library.RefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.RefreshLayoutDirection;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 搜索车辆
 */
public class SearchCarActivity extends BaseActivity implements CarListAdapter.ClickItemInterface, RefreshLayout.OnRefreshListener, LoadingView.OnBtnClickListener, PageView.PageViewInterface {

    @BindView(R.id.pageView)
    PageView pageView;
    @BindView(R.id.edt_search)
    EditText edt_search;
    @BindView(R.id.tv_tittle)
    TextView tv_tittle;
    @BindView(R.id.img_add)
    ImageView img_add;
    @BindView(R.id.img_right)
    ImageView img_right;
    CarListAdapter carListAdapter;
    SearchCarModel searchCarModel;
    int type = 1;

    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_search_car);
    }

    @Override
    public void init() {
        type = getIntent().getIntExtra("type", 1);
        searchCarModel = new SearchCarModel(this);
        carListAdapter = new CarListAdapter(this, null);
        pageView.refreshLayout.setOnRefreshListener(this);
        pageView.setPageViewInterface(this);
        pageView.setBottomInterface(carListAdapter);
        pageView.setReLoadBtnListener(this);
        pageView.setRecyclerViewAdapter(MyViewUtils.getLinearLayoutManager(this), carListAdapter);

        if (type == 1) {
            img_add.setVisibility(View.VISIBLE);
            img_right.setVisibility(View.VISIBLE);
            tv_tittle.setText("我的车辆");
            searchCarModel.getList(edt_search, pageView, carListAdapter);
        } else {
            img_add.setVisibility(View.INVISIBLE);
            img_right.setVisibility(View.INVISIBLE);
            tv_tittle.setText("搜索车辆");
            edt_search.setFocusable(true);
            edt_search.setFocusableInTouchMode(true);
            edt_search.requestFocus();
        }

    }

    @OnClick(R.id.img_back)
    void back() {
        finish();
    }

    @OnClick(R.id.img_add)
    void add() {
        openActivity(AttentionActivity.class);
    }

    @OnClick(R.id.img_right)
    void openCarMap() {
        openActivity(MapCarActivity.class);
    }

    @OnClick(R.id.btn_search)
    void search() {
        searchCarModel.getList(edt_search, pageView, carListAdapter);
    }

    @Override
    public void loadData(int dataPage) {
        searchCarModel.getList(edt_search, pageView, carListAdapter);
    }

    @Override
    public void openDetail(String plate, String vin, int type) {
        Bundle bundle = new Bundle();
        bundle.putString("plate", plate);
        bundle.putString("vin", vin);
        bundle.putInt("type", type);
        openActivity(CarDetailActivity.class, bundle);
    }

    @Override
    public void onReload() {
        searchCarModel.getList(edt_search, pageView, carListAdapter);
    }

    @Override
    public void onRefresh(RefreshLayoutDirection direction) {
        pageView.refreshData(direction);
    }

}
